var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: -1,
    i: 0,
    type:"",
    link:"",
    sickrela: [{
        relation: '家庭成员',
        index: 1
      },
      {
        relation: '亲戚',
        index: 2
      },
      {
        relation: '朋友',
        index: 3
      },
      {
        relation: '其他',
        index: 4
      }
    ],
    time:"",
    week:"",
    timeText_start:"",
    timeText_end:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var zid = options.zid
    var tid = options.tid;
    var inquiry = options.inquiry
    var ifzy = options.ifzy
    var last = options.last;
    var leixing = options.leixing;
    console.log(options)
    var type = options.type;
    var link = options.link;
    if(type != undefined && link != 'undefined')
    {
      that.setData({
        type:type
      })
    }
    if(link != undefined && link != 'undefined')
    {
      that.setData({
        link:link
      })
    }
    if (options.gren) {
      var gren = options.gren
      that.setData({
        gren: gren
      })
    }
    if (last) {
      that.setData({
        last: last
      })
    } else {
      that.setData({
        last: ''
      })
    }
    if (options.tjorder) {
      that.setData({
        tjorder: options.tjorder
      })
    }
    if (options.tijianper) {
      var tijianper = options.tijianper
      that.setData({
        tijianper: tijianper
      })
    }
    if (options.arr) {
      var arr = JSON.parse(options.arr)
      that.setData({
        arr: arr
      })
    }
    if (options.doc == 1) {
      var openid = options.openid
      that.setData({
        openid: openid,
        doc: options.doc
      })
    } else {
      var openid = wx.getStorageSync('openid')
      that.setData({
        openid: openid,
        doc: ""
      })
    }
    var addnum = options.addnum
    if (options.inquiry == '-1') {
      var back_orser = options.back_orser
      var inquiry = options.inquiry
      that.setData({
        back_orser: back_orser,
        inquiry: inquiry
      })
    }
    //是否存在处方类型
    if (options.exist) {
      that.setData({
        exist: options.exist
      })
    }
    if (options.type) {
      that.setData({
        type: options.type
      })
    }
    if (options.tit) {
      var tit = options.tit
      that.setData({
        tit: options.tit
      })
    }
    var time = options.time;
    var timeText_start = options.timeText_start;
    var timeText_end = options.timeText_end;
    if(time)
    {
      that.setData({
        time:time,
      })
    }
    if(timeText_start)
    {
      that.setData({
        timeText_start:timeText_start,
      })
    }
    if(timeText_end)
    {
      that.setData({
        timeText_end:timeText_end,
      })
    }
    if (options.key_words) {
      if (options.conets) {
        var conets = JSON.parse(options.conets)
      } else {
        var conets = ''
      }

      var key_words = options.key_words
      var cfstate = options.cfstate
      var money = options.money
      var time_leng = options.time_leng
      var phone = options.phone
      var data_time = options.data_time
      var month_time = options.month_time
      var week = options.week
      if (options.textarr != undefined) {
        var textarr = JSON.parse(options.conets)
        var text = textarr.text
      }

      that.setData({
        zid: zid,
        conets: conets,
        key_words: key_words,
        cfstate: cfstate,
        money: money,
        time_leng: time_leng,
        phone: phone,
        data_time: data_time,
        month_time: month_time,
        week: week,
        addnum: addnum,
        text: text,
        ifzy: ifzy
      })
    } else if (leixing == 'lvtong') {
      that.setData({
        did: options.did,
        server: options.server,
        money: options.money,
        hid: options.hid,
        keshi_two: options.keshi_two,
        typeid: options.tid,
        leixing: options.leixing,
        conets: options.conets,
        cfstate: options.cfstate,
        arr: options.arr,
        time: options.time,
        zid: options.zid,
        week:options.week
      })
      if (options.fuwus != '' && options.fuwus != undefined) {
        that.setData({
          fuwus: options.fuwus
        })
      }
    }
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })

    that.setData({
      bgc: bgc,
      tid: tid,
      inquiry: inquiry
    })
    that.getMyalluser()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  next() {
    var that = this
    var msg = that.data.msg
    var j_id = that.data.j_id
    var zid = that.data.zid
    var tid = that.data.tid
    var ifzy = that.data.ifzy
    var cfstate = that.data.cfstate
    var conets = JSON.stringify(that.data.conets)
    var money = that.data.money
    var key_words = that.data.key_words
    var time_leng = that.data.time_leng
    var phone = that.data.phone
    var data_time = that.data.data_time
    var week = that.data.week
    var month_time = that.data.month_time
    var addnum = that.data.addnum
    var arr = JSON.stringify(that.data.arr)
    //(arr)
    var text = that.data.text
    var inquiry = that.data.inquiry
    var tijianper = that.data.tijianper
    var fuwus = that.data.fuwus;
    //(tijianper)
    if (tijianper == '1') {
      var names = that.data.names
      var numcard = that.data.numcard
      var sex = that.data.sex
      var tel = that.data.tel
      var region = that.data.region
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //
      var j_id = that.data.j_id
      prevPage.setData({
        names: names,
        numcard: numcard,
        sex: sex,
        tel: tel,
        region: region,
        j_id: j_id
      })
      wx.navigateBack({
        detail: 1
      })
      return false
    }
    var timeText_start = that.data.timeText_start;
    var timeText_end = that.data.timeText_end;
    var time = that.data.time;
    if (that.data.exist) {
      var exist = that.data.exist
    }

    if (tid != '' && tid != undefined && tid != 'undefined' && that.data.leixing != 'lvtong') {

      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifus/huanzhezhifus?inquiry=' + that.data.inquiry + '&ser_key=' + key_words + '&pinyin=' + key_words + '&tid=' + tid + '&key_words=' + key_words + '&j_id=' + j_id + '&addnum=' + addnum + '&money=' + money,
      })
      return false;
    }
    if (that.data.inquiry == '-1') {
      console.log('1')
      //跳转处方
      var tit = that.data.tit
      var back_orser = that.data.back_orser
      var zid = that.data.zid
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?inquiry=' + that.data.inquiry + '&conets=' + conets + '&ser_key=' + key_words + '&pinyin=' + key_words + '&tit=' + tit + '&j_id=' + j_id + '&back_orser=' + back_orser + '&zid=' + zid + '&cfstate='+that.data.cfstate + '&key_words=' + key_words + '&addnum=' + addnum + '&arr=' + arr,
      })
      return false;
    } else {
      console.log(key_words, inquiry, ifzy)
      if (inquiry == 4) {
        // wx.navigateTo({
        //   url: '/hyb_yl/backstageServices/pages/xuanzeyaopin/xuanzeyaopin?key_words=' + key_words + '&j_id=' + j_id + '&ser_key=' + key_words + '&pinyin=' + key_words + '&inquiry=' + this.data.inquiry + '&conets=' + conets + '&text=' + text,
        // })
        wx.navigateTo({
          url: '/hyb_yl/backstageServices/pages/yaotang/yaotang?key_words=' + key_words + '&j_id=' + j_id + '&ser_key=' + key_words + '&pinyin=' + key_words + '&inquiry=' + this.data.inquiry + '&conets=' + conets + '&text=' + text,
        })
        return false;
      }
      if (key_words == 'tuwenwenzhen' || key_words == 'tijianjiedu') {
        if (that.data.tjorder) {
          wx.reLaunch({
            url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?key_words=' + key_words + '&zid=' + zid + '&j_id=' + j_id + '&cfstate=' + cfstate + '&conets=' + conets + '&money=' + money + '&time_leng=' + time_leng + '&phone=' + phone + '&addnum=' + addnum + '&arr=' + arr + '&tjorder=' + that.data.tjorder,
          })
        } else {
          wx.reLaunch({
            url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?key_words=' + key_words + '&zid=' + zid + '&j_id=' + j_id + '&cfstate=' + cfstate + '&conets=' + conets + '&money=' + money + '&time_leng=' + time_leng + '&phone=' + phone + '&addnum=' + addnum + '&arr=' + arr,
          })
        }
      }
      if (key_words == "dianhuajizhen" || key_words == "shipinwenzhen" || key_words == 'shoushukuaiyue' || key_words == 'yuanchengguahao') {

        wx.redirectTo({
          url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?key_words=' + key_words + '&zid=' + zid + '&j_id=' + j_id + '&cfstate=' + cfstate + '&conets=' + conets + '&money=' + money + '&time_leng=' + time_leng + '&phone=' + phone + '&data_time=' + data_time + '&month_time=' + month_time + '&week=' + week + '&addnum=' + addnum + '&arr=' + arr,
        })
      }
      if (key_words == "yuanchengkaifang" && inquiry == 3 && ifzy == 1) {
        var tit = that.data.tit
        wx.navigateTo({
          url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?key_words=' + key_words + '&conets=' + conets + '&zid=' + zid + '&j_id=' + j_id + '&typs=0' + '&money=' + money + '&addnum=' + addnum + '&ser_key=' + key_words + '&arr=undefined',
        })
        return false;
      }


      if (key_words == "yuanchengkaifang" && inquiry == 3 && ifzy == 0) {
        var tit = that.data.tit
        wx.navigateTo({
          url: '/hyb_yl/userLife/pages/faxian/faxian?inquiry=' + this.data.inquiry + '&conets=' + conets + '&ser_key=' + key_words + '&pinyin=' + key_words + '&tit=' + tit + '&j_id=' + j_id + '&addnum=' + addnum + '&arr=' + arr,
        })
        return false;
      }
      if (that.data.leixing == 'lvtong') {
        wx.navigateTo({
          url: '/hyb_yl/userCommunicate/pages/orders/orders?did=' + that.data.did + "&server=" + that.data.server + "&money=" + that.data.money + "&hid=" + that.data.hid + "&keshi_two=" + that.data.keshi_two + "&tid=" + that.data.typeid + "&leixing=" + that.data.leixing + '&conets=' + that.data.conets + '&cfstate=' + that.data.cfstate + '&arr=' + that.data.arr + '&time=' + that.data.time + '&j_id=' + j_id + '&fuwus=' + fuwus + '&zid=' + zid+ '&timeText_start='+that.data.timeText_start+'&timeText_end='+that.data.timeText_end+'&time='+that.data.time+'&week='+that.data.week,
        })
        return false;
      }

    }

    // if (key_words < 4 || key_words == 8) {
    //   wx.navigateTo({
    //     url: '/hyb_yl/zhuanjiasubpages/pages/huanzhezhifu/huanzhezhifu?inquiry=' + key_words + '&msg=' + msg + '&j_id=' + j_id + '&data_time=' + data_time + '&month_time=' + month_time + '&week=' + week + '&phone=' + phone + '&zid=' + zid,
    //   })
    // } else if (inquiry == 4) {
    //   wx.navigateTo({
    //     url: '/hyb_yl/backstageServices/pages/seldrup/seldrup?inquiry=' + key_words,
    //   })
    // } else if (key_words == 5) {
    //   wx.navigateTo({
    //     url: '/hyb_yl/mysubpages/pages/report/report',
    //   })
    // } else if (key_words == 6) {
    //   wx.navigateTo({
    //     url: '/hyb_yl/mysubpages/pages/report_detail/report_detail',
    //   })
    // } else if (key_words == 9) {
    //   wx.navigateTo({
    //     url: '/hyb_yl/backstageLife/pages/daozhenpay/daozhenpay',
    //   })
    // }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMyown()
    this.getMyalluser()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  addPeo(e) {
    var sickrela = JSON.stringify(this.data.sickrela)
    var sick_index = e.currentTarget.dataset.sick_index
    console.log(sick_index)
    wx.navigateTo({
      url: "/hyb_yl/userCommunicate/pages/preindex/sickinfo/index?sickrela=" + sickrela + '&sick_index=' + sick_index + '&last=' + this.data.last
    })
  },
  getMyown() {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    var openid = that.data.openid

    var names = userInfo.u_name
    app.util.request({
      'url': 'entry/wxapp/user.myinformation',
      data: {
        names: names,
        openid: openid
      },
      success: function (res) {
        console.log(res)
        if (res.data) {
          var sick_index = 1
        } else {
          var sick_index = 0
        }
        console.log('aaa', res.data)
        that.setData({
          use_own: res.data,
          j_id: res.data.j_id,
          region: res.data.region,
          sex: res.data.sex,
          names: res.data.names,
          numcard: res.data.numcard,
          tel: res.data.tel,
          age: res.data.age,
          sick_index: sick_index
        })
      }
    })
  },
  getMyalluser() {
    var that = this
    //检查是否存在一条自己的信息
    app.util.request({
      'url': 'entry/wxapp/user.alluserfamily',
      data: {
        openid: wx.getStorageSync("openid")
      },
      success: function (res) {
        console.log(res)
        if (res.data.length > 0) {
          that.setData({
            archivesArr: res.data,
            j_id: res.data[0].j_id
          })
        }

      },
      fail: function (err) {
        //(err)
      },
    })
    app.util.request({
      'url': 'entry/wxapp/user.is_one',
      data: {
        openid: wx.getStorageSync("openid")
      },
      success: function (res) {
        if(res.data)
        {
          var list = [{
            relation: '家庭成员',
            index: 1
          },
          {
            relation: '亲戚',
            index: 2
          },
          {
            relation: '朋友',
            index: 3
          },
          {
            relation: '其他',
            index: 4
          }
        ];
          that.setData({
            sickrela:list
          })
        }else{
          var list = [{
            relation: '本人',
            index: 0
          },{
            relation: '家庭成员',
            index: 1
          },
          {
            relation: '亲戚',
            index: 2
          },
          {
            relation: '朋友',
            index: 3
          },
          {
            relation: '其他',
            index: 4
          }
        ];
          that.setData({
            sickrela:list
          })
        }
        

      },
      fail: function (err) {
        //(err)
      },
    })
  },
  detalmsg(e) {
    var that = this
    var j_id = e.currentTarget.dataset.j_id
    var titlme = e.currentTarget.dataset.names
    var openid = e.currentTarget.dataset.openid
    var type = that.data.type;
    var link = that.data.link;
    if(link != '' && link != undefined && link != 'undefined')
    {
      link = link + '?openid='+wx.getStorageSync('openid')+'&names='+titlme+'&j_id='+j_id+'&open=1';
    }
    
    var url = '/hyb_yl/zhuanjiasubpages/pages/danganList/danganList?j_id=' + j_id + '&doc=0' + '&type=1' + '&titlme=' + titlme + '&openid=' + openid;
    //var url1='/hyb_yl/userCommunicate/pages/preindex/sickinfo/index?j_id=' + j_id + '&doc=' + that.data.doc;
    if(type == 'equipment' && link != '' && link != undefined && link != 'undefined')
    {
      wx.navigateTo({
        url: link,
      })
    }else if(type == 'equipment'){
      wx.navigateTo({
        url: '/hyb_yl/pharmacy/pages/scan_add/scan_add?j_id='+j_id,
      })
    }else if(type == 'lvtong' || type=='pingjia' || type == 'fuwu' || type == 'index')
    {
      var pages = getCurrentPages();
      var prePage = pages[pages.length-2];
      prePage.setData({
        j_id:j_id,
        names:e.currentTarget.dataset.names,
        age:e.currentTarget.dataset.age,
        tizhong:e.currentTarget.dataset.tizhong,
        sex:e.currentTarget.dataset.sex,
      })
      wx.navigateBack({})
    }else{
      wx.navigateTo({
        url: url,
      })
    }
    
  },

  choosepeo(e) {
    var that = this
    var sick_index = e.currentTarget.dataset.sick_index
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.keywords
    var numcard = e.currentTarget.dataset.numcard
    var sex = e.currentTarget.dataset.sex
    var names = e.currentTarget.dataset.names,
      j_id = e.currentTarget.dataset.j_id,
      tel = e.currentTarget.dataset.tel,
      region = e.currentTarget.dataset.region;
    var i = (e.currentTarget.dataset.ind)
    if (i !== 'undefined') {
      that.setData({
        i: i,
        j_id: j_id,
        names: names,
        numcard: numcard,
        sex: sex,
        tel: tel,
        region: region
      })
    } else {
      that.setData({
        i: -1,
        j_id: j_id,
        names: names,
        numcard: numcard,
        sex: sex,
        tel: tel,
        region: region
      })
    }

  }
})